<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Http;
use think\Log;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $yesterday = date('Ymd',bcsub(time(),86400));
        $this->success('请求成功',$yesterday);
    }

    public function shenhe()
    {
        if ($this->auth->isLogin()){
            $this->success('',true);
        }
        $this->success('',true);
    }

    public function suiji()
    {
        $this->success('',rand(0,500)/100);
    }

    // 巨量广告回调
    public function req()
    {
        Log::error('后台巨量');
        $event_type = input('event_type');
        $clickid = input('clickid');
        $time = input('time',time());
        $time = $time * 1000;

        $data = '{
            "event_type": "'.$event_type.'",
            "context":
            {
                "ad": {"callback": "'.$clickid.'"}
            },
            "timestamp": '.$time.'
        }';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://analytics.oceanengine.com/api/v2/conversion");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
        ));
        $result = curl_exec($ch);
        curl_close($ch);

        $this->success('',$result);
    }

    public function tengxun()
    {
        $param = input();

        db('ad_tf_jc')->insert([
            "content" => json_encode($param,JSON_UNESCAPED_UNICODE),
            "createtime"=> time(),
        ]);

        return "ok";
    }

    public function kuaishou()
    {
        return "ok";
    }

    // 磁力广告回调
    public function cilireq()
    {

        $callback = input('callback');
        $event_time = input('event_time');
        $event_type = input('event_type');
        $purchase_amount = input('purchase_amount');

        $data = [
            "callback" => $callback,
            "event_time" => $event_time,
            "event_type" => $event_type,
            "purchase_amount" => $purchase_amount,
        ];

        $result = Http::get("https://ad.partner.gifshow.com/track/activate",$data);

        $this->success('',$result);
    }


    public function detail_button()
    {
        $this->success('',true);
    }

    public function jieguo()
    {
        $click_id = input('clickid');
        $event_type = input('event_type');
        $platform = input('platform');
        $jieguo = input('jieguo');

        $data = [
            'clickid' => $click_id,
            'event_type' => $event_type,
            'platform' => $platform,
            'content' => $jieguo,
            'createtime' => time(),
        ];

        db('ad_tf_result')->insert($data);

        $this->success('');
    }
}
